var player = null;

$(function(){
    player = createPlayer();
    player.on('ready',function(){
      console.log('ready可以调用播放器的方法了');
    });

    player.on('play',function(){
      console.log('开始播放(play)');
    });

    player.on('pause',function(){
       console.log('暂停播放(pause)');
     });

    $('.change').on('click',function(){
        var source = $('.source').val();
        var playAuth = $('.playauth').val();
        player = createPlayer(source,playAuth);
    })

    $('.submit').on('click',function(){
        console.info("提交行为触发");
        var source = $('.source').val();
        var playAuth = $('.playauth').val();
        console.info("playauth: " + playAuth);
        if(!source)
        {
            console.info("!source")
            return;
        }
        if(source.indexOf('//')!=-1)
        {
            player.loadByUrl(source);
        }
        else if(playAuth)
        {
            console.info(playAuth);
            if(player.replayByVidAndPlayAuth)
            {
                player.replayByVidAndPlayAuth(source, playAuth);
            }
            else
            {
                player = createPlayer(source,playAuth);
            }
        }
    })
})

function createPlayer(source, playauth)
{
    if(player)
    {
        player.dispose();
        $('#J_prismPlayer').empty();
        player = null;
    }
    var vid = source;
    if(!source && !playauth)
    {
        source = '';
        vid = "";
        playauth = "";
    }
    else if(source.indexOf('//')!=-1)
    {
        playAuth = "";
    }
    else if(playauth)
    {
        source = "";
    }
    var option = {
    id: "J_prismPlayer",
         autoplay: false,
         isLive:false, //  是否直播，直播过程中不允许用户拖动进度条
         playsinline:false,
         width:"100%",
         height:"100%",
         controlBarVisibility:"click",
         useH5Prism:true, //启用H5播放器
         useFlashPrism:false, // 是否启用falsh播放器
         source:source,
         vid:vid,
         playauth:playauth,
         cover:"",
         "language":"zh-cn",
         "controlBarVisibility":"hover"              
    };

    return new Aliplayer(option);
}